home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
wx
/
py
/
dispatcher.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
7KB
|
267 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
__cvsid__ = '$Id: dispatcher.py 39667 2006-06-11 00:13:05Z RD $'
__revision__ = '$Revision: 39667 $'[11:-2]
import exceptions
import types
import weakref
class DispatcherError(exceptions.Exception):
def __init__(self, args = None):
self.args = args
class Parameter:
def __repr__(self):
return self.__class__.__name__
class Any(Parameter):
pass
Any = Any()
class Anonymous(Parameter):
pass
Anonymous = Anonymous()
connections = { }
senders = { }
_boundMethods = weakref.WeakKeyDictionary()
def connect(receiver, signal = Any, sender = Any, weak = True):
if signal is None:
raise DispatcherError, 'signal cannot be None'
if weak:
receiver = safeRef(receiver)
senderkey = id(sender)
signals = { }
if connections.has_key(senderkey):
signals = connections[senderkey]
else:
connections[senderkey] = signals
if sender not in (None, Any):
def remove(object, senderkey = senderkey):
_removeSender(senderkey = senderkey)
try:
weakSender = weakref.ref(sender, remove)
senders[senderkey] = weakSender
receivers = []
if signals.has_key(signal):
receivers = signals[signal]
else:
signals[signal] = receivers
try:
receivers.remove(receiver)
except ValueError:
pass
receivers.append(receiver)
def disconnect(receiver, signal = Any, sender = Any, weak = True):
if signal is None:
raise DispatcherError, 'signal cannot be None'
if weak:
receiver = safeRef(receiver)
senderkey = id(sender)
try:
receivers = connections[senderkey][signal]
except KeyError:
raise DispatcherError, 'No receivers for signal %r from sender %s' % (signal, sender)
try:
receivers.remove(receiver)
except ValueError:
raise DispatcherError, 'No connection to receiver %s for signal %r from sender %s' % (receiver, signal, sender)
_cleanupConnections(senderkey, signal)
def send(signal, sender = Anonymous, **kwds):
senderkey = id(sender)
anykey = id(Any)
receivers = []
try:
receivers.extend(connections[senderkey][signal])
except KeyError:
pass
anyreceivers = []
try:
anyreceivers = connections[senderkey][Any]
except KeyError:
pass
for receiver in anyreceivers:
if receivers.count(receiver) == 0:
receivers.append(receiver)
continue
anyreceivers = []
try:
anyreceivers = connections[anykey][signal]
except KeyError:
pass
for receiver in anyreceivers:
if receivers.count(receiver) == 0:
receivers.append(receiver)
continue
anyreceivers = []
try:
anyreceivers = connections[anykey][Any]
except KeyError:
pass
for receiver in anyreceivers:
if receivers.count(receiver) == 0:
receivers.append(receiver)
continue
responses = []
for receiver in receivers:
if type(receiver) is weakref.ReferenceType or isinstance(receiver, BoundMethodWeakref):
receiver = receiver()
if receiver is None:
continue
response = _call(receiver, signal = signal, sender = sender, **kwds)
responses += [
(receiver, response)]
return responses
def _call(receiver, **kwds):
if hasattr(receiver, '__call__'):
if hasattr(receiver.__call__, 'im_func') or hasattr(receiver.__call__, 'im_code'):
receiver = receiver.__call__
if hasattr(receiver, 'im_func'):
fc = receiver.im_func.func_code
acceptable = fc.co_varnames[1:fc.co_argcount]
elif hasattr(receiver, 'func_code'):
fc = receiver.func_code
acceptable = fc.co_varnames[0:fc.co_argcount]
else:
raise DispatcherError, 'Unknown receiver %s of type %s' % (receiver, type(receiver))
if not fc.co_flags & 8:
for arg in kwds.keys():
if arg not in acceptable:
del kwds[arg]
continue
return receiver(**kwds)
def safeRef(object):
if hasattr(object, 'im_self'):
if object.im_self is not None:
selfkey = object.im_self
funckey = object.im_func
if not _boundMethods.has_key(selfkey):
_boundMethods[selfkey] = weakref.WeakKeyDictionary()
if not _boundMethods[selfkey].has_key(funckey):
_boundMethods[selfkey][funckey] = BoundMethodWeakref(boundMethod = object)
return _boundMethods[selfkey][funckey]
return weakref.ref(object, _removeReceiver)
class BoundMethodWeakref:
def __init__(self, boundMethod):
self.isDead = 0
def remove(object, self = self):
self.isDead = 1
_removeReceiver(receiver = self)
self.weakSelf = weakref.ref(boundMethod.im_self, remove)
self.weakFunc = weakref.ref(boundMethod.im_func, remove)
def __repr__(self):
return '<bound method weakref for %s.%s>' % (self.weakSelf, self.weakFunc)
def __call__(self):
if self.isDead:
return None
else:
object = self.weakSelf()
method = self.weakFunc().__name__
try:
return getattr(object, method)
except AttributeError:
return None
def _removeReceiver(receiver):
for senderkey in connections.keys():
for signal in connections[senderkey].keys():
receivers = connections[senderkey][signal]
try:
receivers.remove(receiver)
except:
pass
_cleanupConnections(senderkey, signal)
def _cleanupConnections(senderkey, signal):
receivers = connections[senderkey][signal]
if not receivers:
signals = connections[senderkey]
del signals[signal]
if not signals:
_removeSender(senderkey)
def _removeSender(senderkey):
del connections[senderkey]
try:
del senders[senderkey]
except:
pass